<template>
	<view class="p-l-40 p-r-40 p-t-40">
		<uni-forms ref="form" :label-width='90' :rules="rules" :modelValue="form">
			<uni-forms-item label="旧密码" name="oldPwd">
				<uni-easyinput type="text" v-model="form.oldPwd" placeholder="请输入旧密码" />
			</uni-forms-item>
			<uni-forms-item label="新密码" name="newPwd">
				<uni-easyinput type="text" v-model="form.newPwd" placeholder="请输入新密码" />
			</uni-forms-item>
			<uni-forms-item label="确认新密码" name="newPwd2">
				<uni-easyinput type="text" v-model="form.newPwd2" placeholder="确认新密码" />
			</uni-forms-item>
		</uni-forms>

		<button class="m-t-100" type="primary" @click="postForm">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					oldPwd: "",
					newPwd: '',
					newPwd2:''
				},
				rules: {
					oldPwd: {
						rules: [{
							required: true,
							errorMessage: '请输入旧密码',
						}, {
							validateFunction: function(rule, value, data, callback) {
								let reg = /^[\S]{6,12}$/
								if (!reg.test(value)) {
									callback('请输入6~12 非特殊字符')
								}
								return true
							}
						}]
					},
					newPwd:{
						rules:[{
							required: true,
							errorMessage: '请输入新密码',
						}]
					},
					newPwd2:{
						rules:[{
							required: true,
							errorMessage: '请输入并与新密码保持一致',
						},{
							validateFunction: function(rule, value, data, callback) {
								if(data.newPwd !== value){
									callback('两次输入不一致')
								}
								return true
							}
						}]
					}
					
				}

			}
		},
		methods: {
			postForm() {
				this.$refs.form.validate().then(res => {
					this.$http.post('/user/updatepwd',this.form).then(res=>{
					   uni.showToast({
					   	title:res.message,
						success() {
							let time = setTimeout(function(){
								uni.navigateBack()
								clearTimeout(time)
							},1500)
						}
					   })
					})
				})
			}
		}
	}
</script>

<style>
	page {
		background-color: white;
	}
</style>
